package org.example.mallcommon.global;


import lombok.extern.slf4j.Slf4j;
import org.example.mallcommon.exceptioninfo.LoginExceptionInfo;
import org.example.mallcommon.exceptioninfo.OssException;
import org.example.mallcommon.exceptioninfo.RegisterException;
import org.example.mallcommon.result.ReturnResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 韩先楚
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 阿里云oss上传异常处理
     */
    @ExceptionHandler(OssException.class)
    public ReturnResult<String> handleOssException(OssException e) {
        log.error("阿里云oss上传异常", e);
        return ReturnResult.error(e.getMessage());
    }

    /**
     * 登录异常处理
     */
    @ExceptionHandler(LoginExceptionInfo.class)
    public ReturnResult handleLoginException(LoginExceptionInfo e) {
        log.error("登录异常", e);
        return ReturnResult.error(e.getMessage());
    }

    /**
     * 注册异常处理
     */
    @ExceptionHandler(RegisterException.class)
    public ReturnResult handleRegisterException(RegisterException e) {
        log.error("注册异常", e);
        return ReturnResult.error(e.getMessage());
    }


    /**
     * quarkus 未知异常处理
     */
    @ExceptionHandler(Exception.class)
    public ReturnResult handleException(Exception e) {
        log.error("未知异常", e);
        return ReturnResult.error("操作失败，请稍后再试");
    }
}
